package com.xbongbong.paas.controller.workflow;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.workflow.task.dto.WorkFlowTaskLogDetailDTO;
import com.xbongbong.paas.pojo.workflow.task.dto.WorkFlowTaskLogListDTO;
import com.xbongbong.paas.pojo.workflow.task.vo.WorkFlowTaskLogListVO;
import com.xbongbong.paas.service.workflow.log.WorkFlowTaskLogService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @description: 工作流设置相关
 * @author 吴峰
 * @date 2021/5/10 14:44
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/workflowTaskLog")
public class WorkflowTaskLogController {

    @Resource
    private WorkFlowTaskLogService workFlowTaskLogService;

    /**
     *
     * @param workFlowTaskLogListDTO
     * @param br
     * @return
     * @throws
     * @author huguojun
     * @date 2021/5/10 14:45
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid WorkFlowTaskLogListDTO workFlowTaskLogListDTO, BindingResult br) {
        XbbResponse<WorkFlowTaskLogListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkFlowTaskLogListVO workFlowTaskLogListVO = workFlowTaskLogService.list(workFlowTaskLogListDTO);
                response = new XbbResponse<>(workFlowTaskLogListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     *
     * @param workFlowTaskLogDetailDTO
     * @param br
     * @return
     * @throws
     * @author huguojun
     * @date 2021/5/10 14:45
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String detail(@RequestBody @Valid WorkFlowTaskLogDetailDTO workFlowTaskLogDetailDTO, BindingResult br) {
        XbbResponse<WorkFlowTaskLogListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkFlowTaskLogListVO workFlowTaskLogListVO = workFlowTaskLogService.detail(workFlowTaskLogDetailDTO);
                response = new XbbResponse<>(workFlowTaskLogListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
